package com.qinggeng.project.system.service.impl;

import com.google.common.collect.Lists;
import com.qinggeng.common.enums.JobUserTypeEnum;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.project.api.dto.JobMessageDTO;
import com.qinggeng.project.system.domain.JobDlivery;
import com.qinggeng.project.system.domain.JobMessage;
import com.qinggeng.project.system.domain.JobUser;
import com.qinggeng.project.system.domain.vo.JobMessagePositionVO;
import com.qinggeng.project.system.domain.vo.JobMessageResumeVO;
import com.qinggeng.project.system.mapper.JobMessageMapper;
import com.qinggeng.project.system.mapper.JobUserMapper;
import com.qinggeng.project.system.service.IJobUserService;
import com.qinggeng.project.system.service.JobMessageService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class JobMessageServiceImpl implements JobMessageService {
    @Resource
    private JobMessageMapper jobMessageMapper;

    @Resource
    private JobUserMapper jobUserMapper;
    @Autowired
    private IJobUserService jobUserService;

    @Override
    public boolean sendMessageCompany(JobMessage jobMessage) {
        Long positionUserId=jobMessage.getPositionUserId();
        if (null ==positionUserId){
            throw  new CustomException("positionUserId is null");
        }
        Long resumeUserId=jobMessage.getResumeUserId();
        if (null ==resumeUserId){
            throw  new CustomException("resumeUserId is null");
        }
        return insertJobMessage(jobMessage);
    }

    @Override
    public boolean sendMessageJobUser(JobMessage jobMessage) {

        return insertJobMessage(jobMessage);
    }

    @Override
    public List<JobMessageResumeVO> listJobMessageByResumeUserId() {
        Long jobUserId=jobUserService.getJobUserId();
        JobMessage  jobMessage =new JobMessage();
        jobMessage.setResumeUserId(jobUserId);
        return jobMessageMapper.listJobMessageByResumeUserId(jobMessage);
    }

    @Override
    public List<JobMessagePositionVO> listJobMessageByPositionUserId() {
        Long jobUserId=jobUserService.getJobUserId();
        JobMessage  jobMessage =new JobMessage();
        jobMessage.setResumeUserId(jobUserId);
        return jobMessageMapper.listJobMessageByPositionUserId(jobMessage);
    }



    private boolean insertJobMessage(JobMessage jobMessage){
        Long positionUserId=jobMessage.getPositionUserId();
        if (null ==positionUserId){
            throw  new CustomException("positionUserId is null");
        }
        Long resumeUserId=jobMessage.getResumeUserId();
        if (null ==resumeUserId){
            throw  new CustomException("resumeUserId is null");
        }
        Date date =new Date();
        jobMessage.setCreateTime(date);
        jobMessage.setUpdateTime(date);
        jobMessage.setDeliveryDate(date);
        jobMessage.setDelFlag(0);
        jobMessage.setStatus(0);
        jobMessage.setIsRead(0);
        return  jobMessageMapper.insertSelective(jobMessage)>0;
    }


    @Override
    public Integer getJobMessageCountByPositionUserId() {
        JobMessage where=new JobMessage();
        where.setPositionUserId(jobUserService.getJobUserId());
        where.setType(Integer.parseInt(JobUserTypeEnum.POSITION_USER.getType()));
        return jobMessageMapper.getJobMessageCountByJobUserIdAndType(where);
    }

    @Override
    public Integer getJobMessageCountByResumeUserId() {
        JobMessage where=new JobMessage();
        where.setResumeUserId(jobUserService.getJobUserId());
        where.setType(Integer.parseInt(JobUserTypeEnum.RESUME_USER.getType()));
        return jobMessageMapper.getJobMessageCountByJobUserIdAndType(where);
    }
}
